// ====================================================
//
// This file is part of the CSCEC81 Cloud Platform.
//
// Create by CSCEC81 Technology <support@cscec81.com>
// Copyright (c) 2020-2021 cscec81.com
//
// ====================================================

package com.cscec81.passport.base.service.oauth2.utils;

import com.cscec81.passport.base.commons.utils.EncryptUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;


public class DESPasswordEncoder implements PasswordEncoder {

  private EncryptProperties properties;
  public DESPasswordEncoder(EncryptProperties properties){
    this.properties = properties;
  }

  @Override
  public String encode(CharSequence rawPassword) {
    String data = (String) rawPassword;

    try {
      if (properties == null) {
        properties = new EncryptProperties();
        properties.setKey("cscec81.");
      }
      return EncryptUtils.desEncrypt(data, properties.getKey());
    } catch (Exception ex) {
      return null;
    }
  }

  @Override
  public boolean matches(CharSequence rawPassword, String encodedPassword) {

    try {
      return encodedPassword.equals(encode(rawPassword));
    } catch (Exception ex) {
      return false;
    }
  }
}
